package com.example.demoserver.controller;

import com.example.demoserver.entity.InteLog;
import com.example.demoserver.service.InteLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/log")
@RequiredArgsConstructor
@Component
@Slf4j
public class InteLogController {
    private final InteLogService inteLogService;
    Integer Ltype=0;
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    @GetMapping("/setexception")
    public void setException(Integer type) {
        if(type==0){
            executor.shutdownNow();
        }else{
            Ltype=type;
            Executors.newSingleThreadScheduledExecutor();
            Random random = new Random();
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    InteLog inteLog=exceptions(Ltype,"111");
                    inteLog.setLogtime(Util.getNowTime());
                    inteLogService.save(inteLog);
                    int delay = random.nextInt(11) + 10;
                    executor.schedule(this, delay, TimeUnit.SECONDS);
                }
            };
            executor.submit(task);
        }

    }
    @GetMapping("/exceptios")
    public Integer getExceptios() {
        return Ltype;
    }
    public  InteLog exceptions(Integer type,String info){
        InteLog inteLog=new InteLog();
        inteLog.setLogtype(type);
        switch (type) {
            case 0:
                inteLog.setLoginfo("无异常");
                break;
            case 1:
                inteLog.setLoginfo("运行状态异常");
                break;
            case 2:
                inteLog.setLoginfo("通信异常");
                break;
            case 3:
                inteLog.setLoginfo("安全漏洞异常");
                break;
            case 4:
                inteLog.setLoginfo("版本过低异常");
                break;
            case 5:
                inteLog.setLoginfo("微服务状态异常");
                break;
        }
            return inteLog;
    }

}
